//
//  FLLDetailViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/9.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLDetailViewController.h"
#import "ZWYNetTool.h"
#import "FLLDetailModel.h"
#import "FLLDetailData.h"
#import "FLLDetailSecData.h"
#import "FLLMacro.h"
#import "Masonry.h"
#import "FLLSpeakTableViewCell.h"
#import "FLLCommentModel.h"
#import "UIImageView+WebCache.h"
#import "CommentData.h"
#import "CommentParents.h"
#import "CommentSecData.h"
#import "UILabel+FLLSetPropety.h"
#import "FLLAgreeModel.h"
#import "AgreeData.h"
#import "AgreeSecData.h"
#import "UIView+Frame.h"
#import "FLLChoseCollectionViewCell.h"
#import "FLLCourseRelateModel.h"
#import "CourseRelateData.h"
#import "CourseRelateSecData.h"
#import "CourseRelation.h"
#import "FLLAgreeCollectionViewCell.h"
#import "FLLRelatedCourseCollectionViewCell.h"


@interface FLLDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, copy) NSString *idString;
/* 上面 */
@property (nonatomic, strong) FLLDetailModel *fllDetailModel;
@property (nonatomic, strong) FLLDetailData *topDataSource;
@property (nonatomic, strong) UITableView *detailTableView;
@property (nonatomic, strong) NSMutableArray<FLLDetailSecData *> *topArray;

/* 评论 */
@property (nonatomic, strong) FLLCommentModel *fllCommentModel;
@property (nonatomic, strong) NSMutableArray<CommentSecData *> *speakArray;
@property (nonatomic, strong) NSMutableArray<CommentParents *> *parentsArray;
@property (nonatomic, assign) NSInteger addHeight;
/* 传值参数 */
@property (nonatomic, copy) NSString *bodyString;
@property (nonatomic, copy) NSString *titleString;
@property (nonatomic, copy) NSString *string;
/* 刷新加载 */
@property (nonatomic, assign) BOOL isLoading;
@property (nonatomic, assign) NSInteger pageCount;

/* headerView */
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *dishImageView;
@property (nonatomic, strong) UIImageView *blackView;
@property (nonatomic, strong) UIButton *vidoButton;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UILabel *inClassCount;
@property (nonatomic, strong) UIButton *loveButton;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *expendButton;
@property (nonatomic, strong) UILabel *choseLabel;
@property (nonatomic, strong) UILabel *updateLabel;
@property (nonatomic, strong) UIButton *updateButton;
@property (nonatomic, strong) UICollectionView *choseCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *choseLayout;
@property (nonatomic, assign) CGFloat itemWidth;
@property (nonatomic, assign) CGFloat itemHeight;
@property (nonatomic, strong) UILabel *agreeCount;
@property (nonatomic, strong) UIButton *agreeButton;
@property (nonatomic, strong) UICollectionView *agreeCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *agreeLayout;
@property (nonatomic, strong) UIButton *moreAgreeButton;
@property (nonatomic, strong) UILabel *relatedCourse;
@property (nonatomic, strong) UIButton *moreCourseButton;
@property (nonatomic, strong) UICollectionViewFlowLayout *courseLayout;
@property (nonatomic, strong) UICollectionView *courseCollectionView;
@property (nonatomic, strong) UIView *speakView;
@property (nonatomic, strong) UILabel *speakLabel;
@property (nonatomic, strong) UIButton *cameraButton;
@property (nonatomic, strong) UILabel *homeworkLabel;
@property (nonatomic, strong) UIButton *speakButton;
@property (nonatomic, assign) CGFloat contentLabelHeight;
@property (nonatomic, assign) CGFloat choseCollectionHeight;
@property (nonatomic, assign) BOOL isExpend;
@property (nonatomic, assign) BOOL isUpdate;

/* 点赞数据源 */
@property (nonatomic, strong) FLLAgreeModel *fllAgreeModel;
@property (nonatomic, strong) NSMutableArray<AgreeSecData *> *userInfoArray;

/* 相关课程数据源 */
@property (nonatomic, strong) FLLCourseRelateModel *fllCourseRelateModel;
@property (nonatomic, strong) NSMutableArray<CourseRelateSecData *> *courseArray;

@end

@implementation FLLDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self createPOSTRequest];
    [self createCommentRequest];
    [self createDetailTableView];
    // 创建头视图
    [self createHeaderView];
    
    __weak FLLDetailViewController *weakSelf = self;
    self.block = ^(){
        // 导航标题赋值

            weakSelf.titleString = [NSString stringWithString:[weakSelf.topDataSource.series_name componentsSeparatedByString:@"#"][1]];
            weakSelf.navigationItem.title = weakSelf.titleString;
    };


    // headerView 赋值
    self.headerBlock = ^(){
        
        // 菜品图片
        [weakSelf.dishImageView sd_setImageWithURL:[NSURL URLWithString:weakSelf.topArray.lastObject.course_image] placeholderImage:[UIImage imageNamed:@"菜谱详情加载"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        // 上课人数
        weakSelf.inClassCount.text = [NSString stringWithFormat:@"上课人数:%ld", weakSelf.topArray.lastObject.video_watchcount];
        // 标题
        weakSelf.titleLabel.text = weakSelf.topArray.lastObject.course_name;
        // 具体内容
        weakSelf.contentLabel.text = weakSelf.topArray.lastObject.course_subject;
        // 更新集数
        weakSelf.updateLabel.text = [NSString stringWithFormat:@"更新至%ld集", weakSelf.topArray.count];
         weakSelf.choseLabel.text = @"选集";
        [weakSelf createDianZanRequestWithIndex:weakSelf.topArray.lastObject.course_id];
        [weakSelf createRelatedCourseRequestWithIndex:weakSelf.topArray.lastObject.course_id];
        // 更新choseCollectionView
        [weakSelf.choseCollectionView reloadData];
        
        if ( self.topArray.count <= 8 && self.topArray.count > 0) {
            [weakSelf.choseCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(weakSelf.itemHeight);
            }];
            weakSelf.choseCollectionHeight =  weakSelf.itemHeight;
        } else  {
            [weakSelf.choseCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
                weakSelf.choseCollectionHeight = 2 * weakSelf.itemHeight + CellMargin_5;
                make.height.mas_equalTo(2 * self.itemHeight + CellMargin_5);
            }];
        }
        // 改变headerView高度
        weakSelf.headerView.fll_height = 800 * kHeightScale + weakSelf.choseCollectionHeight;
        // 觉得很赞个数
        weakSelf.agreeCount.font = [UIFont systemFontOfSize:15 * kWidthScale];
        // 相关课程
        weakSelf.relatedCourse.text = @"相关课程";
        weakSelf.relatedCourse.font = [UIFont systemFontOfSize:15 * kWidthScale];
        // 交作业
        weakSelf.homeworkLabel.text = @"交作业";
        // 课堂发言
        [weakSelf.speakButton setTitle:@"课堂发言" forState:UIControlStateNormal];
        weakSelf.speakButton.backgroundColor = FLLOrangeColor;
        weakSelf.speakButton.titleLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
        [weakSelf.speakButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        if (weakSelf.topArray.count < 16) {
            [weakSelf.updateButton setImage:[UIImage new] forState:UIControlStateNormal];
            weakSelf.updateButton.enabled = NO;
        } else {
        
            [weakSelf.updateButton setImage:[UIImage imageNamed:@"expend_down"] forState:UIControlStateNormal];
            weakSelf.updateButton.enabled = YES;
        }
        
    };

    self.commentBlock= ^(){
    
        // 发言
        weakSelf.speakLabel.text = [NSString stringWithFormat:@"%@条发言", weakSelf.fllCommentModel.data.total];

    };
    
    
}

- (void)createHeaderView
{

    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 800 * kHeightScale)];
    
    // 菜品图片
    self.dishImageView = [[UIImageView alloc] init];
    [self.headerView addSubview:_dishImageView];
    self.dishImageView.backgroundColor = [UIColor grayColor];
    [self.dishImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(_headerView.mas_width).multipliedBy(0.53);
    }];
    
    self.blackView = [[UIImageView alloc] init];
    [self.dishImageView addSubview:_blackView];
    [self.blackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    self.blackView.image = [UIImage imageNamed:@"detailbg"];
    self.blackView.alpha = 0.5;
    
    // 播放按钮
    self.vidoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.vidoButton setImage:[UIImage imageNamed:@"play-A"] forState:UIControlStateNormal];
    [self.dishImageView addSubview:_vidoButton];
    [self.vidoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.dishImageView.center);
        make.width.height.mas_equalTo(self.dishImageView.mas_height).multipliedBy(0.2);
    }];

    self.topView = [[UIView alloc] init];
    [self.headerView addSubview:_topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.dishImageView.mas_bottom);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(_dishImageView.mas_height).multipliedBy(0.25);
        make.width.mas_equalTo(_headerView.mas_width).multipliedBy(0.9);
    }];
    
    // 上课人数
    self.inClassCount = [[UILabel alloc] init];
    [self.inClassCount setPropetyWithTextColor:[UIColor lightGrayColor] Alignment:0 textFont:[UIFont systemFontOfSize:14 * kWidthScale]];
    [self.topView addSubview:_inClassCount];
    [self.inClassCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(self.topView.mas_width).multipliedBy(0.5);
    }];
    
    // 分享按钮
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareButton setImage:[UIImage imageNamed:@"foodCourse_share"] forState:UIControlStateNormal];
    [self.topView addSubview:self.shareButton];
    [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(self.topView.mas_height).multipliedBy(0.6);
        make.right.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
        
    }];
    
    // 收藏按钮
    self.loveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.loveButton setImage:[UIImage imageNamed:@"foodCourse_collection"] forState:UIControlStateNormal];
    [self.topView addSubview:self.loveButton];
    [self.loveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(self.shareButton.mas_height);
        make.right.mas_equalTo(self.shareButton.mas_left).mas_offset(- 15);
        make.centerY.mas_equalTo(0);
        
    }];
    
    // titleLabel
    self.titleLabel = [[UILabel alloc] init];
    [self.headerView addSubview:_titleLabel];
    [self.titleLabel setPropetyWithTextColor:[UIColor blackColor] Alignment:0 textFont:[UIFont systemFontOfSize:15 * kWidthScale]];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.leading.mas_equalTo(self.inClassCount);
        make.height.mas_equalTo(self.topView.mas_height);
        make.width.mas_equalTo(self.topView.mas_width).multipliedBy(0.8);
    }];
    
    // expendButton
    self.expendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.expendButton setImage:[UIImage imageNamed:@"expend_down"] forState:UIControlStateNormal];
    [self.expendButton addTarget:self action:@selector(didClickedExpendButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.expendButton];
    [self.expendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(self.shareButton.mas_height).multipliedBy(0.8);
        make.trailing.mas_equalTo(self.topView);
        make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
        
    }];

    // contentLabel
    self.contentLabel = [[UILabel alloc] init];
    [self.contentLabel setPropetyWithTextColor:[UIColor lightGrayColor] Alignment:0 textFont:[UIFont systemFontOfSize:13 * kWidthScale]];
    self.contentLabel.numberOfLines = 0;
    [self.headerView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.titleLabel.mas_bottom);
        make.height.mas_equalTo(0);
    }];
    
    // 选集
    self.choseLabel = [[UILabel alloc] init];
    [self.choseLabel setPropetyWithTextColor:[UIColor blackColor] Alignment:0 textFont:[UIFont systemFontOfSize:15 * kWidthScale]];
    [self.headerView addSubview:self.choseLabel];
    [self.choseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.contentLabel.mas_bottom);
        make.height.mas_equalTo(self.titleLabel.mas_height).multipliedBy(0.5);
        
    }];
    
    // 更新按钮
    self.updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.updateButton setImage:[UIImage imageNamed:@"expend_down"] forState:UIControlStateNormal];
    [self.updateButton addTarget:self action:@selector(didClickedUpdateButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:_updateButton];
    [self.updateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.topView);
        make.width.height.mas_equalTo(self.expendButton.mas_width);
        make.centerY.mas_equalTo(self.choseLabel.mas_centerY);
    }];
    
    // 更新到多少集
    self.updateLabel = [[UILabel alloc]init];
    [self.updateLabel setPropetyWithTextColor:[UIColor lightGrayColor] Alignment:2 textFont:[UIFont systemFontOfSize:14 * kWidthScale]];
    [self.headerView addSubview:self.updateLabel];
    [self.updateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.updateButton.mas_left);
        make.height.mas_equalTo(self.choseLabel.mas_height);
        make.top.mas_equalTo(self.choseLabel);
    }];
    
    // 选集collection
    self.choseLayout = [[UICollectionViewFlowLayout alloc] init];
    self.choseCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.choseLayout];
    [self.headerView addSubview:self.choseCollectionView];
    [self.choseCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.choseLabel.mas_bottom);
        make.height.mas_equalTo(0);
    }];
    self.choseLayout.minimumInteritemSpacing = CellMargin_5;
    self.choseLayout.minimumLineSpacing = CellMargin_5;
    self.choseLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.itemWidth = (SCREEN_WIDTH * 0.9 - 7 * CellMargin_5) / 8.0;
    self.itemHeight = _itemWidth;

    self.choseCollectionView.delegate = self;
    self.choseCollectionView.dataSource = self;
    self.choseCollectionView.backgroundColor = [UIColor whiteColor];
    [self.choseCollectionView registerNib:[UINib nibWithNibName:@"FLLChoseCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"choseCollectionCell"];
    
    // 觉得很赞
    self.agreeCount = [[UILabel alloc] init];
    [self.headerView addSubview:self.agreeCount];
    [self.agreeCount mas_updateConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.choseCollectionView.mas_bottom);
        make.height.mas_equalTo(self.choseLabel.mas_height);
        make.width.mas_equalTo(self.topView.mas_width).multipliedBy(0.4);
    }];
    
    // agreeButton
    self.agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headerView addSubview:_agreeButton];
    [self.agreeButton setImage:[UIImage imageNamed:@"点赞"] forState:UIControlStateNormal];
    [self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.agreeCount.mas_right);
        make.height.width.mas_equalTo(self.loveButton.mas_width).multipliedBy(0.6);
        make.centerY.mas_equalTo(self.agreeCount.mas_centerY);
    }];
    
    // agreeCollection
    self.agreeLayout = [[UICollectionViewFlowLayout alloc] init];
    self.agreeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.agreeLayout];
    [self.headerView addSubview:self.agreeCollectionView];
    [self.agreeCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.agreeCount.mas_bottom);
        make.leading.mas_equalTo(self.topView);
        make.height.mas_equalTo(self.topView.mas_height);
        make.width.mas_equalTo(self.topView.mas_width).multipliedBy(0.9);
        
    }];
    
    
    self.agreeLayout.minimumLineSpacing = CellMargin_10;
    self.agreeLayout.minimumInteritemSpacing = 0;
    self.agreeLayout.sectionInset = UIEdgeInsetsMake(CellMargin_5, CellMargin_5, CellMargin_5, CellMargin_5);
    self.agreeLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.agreeCollectionView.backgroundColor = [UIColor whiteColor];
    self.agreeCollectionView.scrollEnabled = NO;
    self.agreeCollectionView.delegate = self;
    self.agreeCollectionView.dataSource = self;
    
    [self.agreeCollectionView registerNib:[UINib nibWithNibName:@"FLLAgreeCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"agreeCollectionCell"];
    
    // moreAgreeButton
    self.moreAgreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headerView addSubview:_moreAgreeButton];
    [self.moreAgreeButton setImage:[UIImage imageNamed:@"ArrowRight"] forState:UIControlStateNormal];
    [self.moreAgreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.topView);
        make.width.mas_equalTo(self.loveButton.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(self.loveButton.mas_width).multipliedBy(0.4);
        make.centerY.mas_equalTo(self.agreeCollectionView.mas_centerY);
    }];
    
    // 相关课程
    self.relatedCourse = [[UILabel alloc] init];
    [self.headerView addSubview:self.relatedCourse];
    [self.relatedCourse mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.agreeCollectionView.mas_bottom);
        make.height.mas_equalTo(self.topView.mas_height);
        
    }];
    
    // 更多课程button
    self.moreCourseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headerView addSubview:self.moreCourseButton];
    [self.moreCourseButton setImage:[UIImage imageNamed:@"ArrowRight"] forState:UIControlStateNormal];
    [self.moreCourseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.moreAgreeButton.mas_width);
        make.height.mas_equalTo(self.moreAgreeButton.mas_height);
        make.centerY.mas_equalTo(self.relatedCourse.mas_centerY);
        make.trailing.mas_equalTo(self.topView);
    }];
    
    // courseCollectionView
    self.courseLayout = [[UICollectionViewFlowLayout alloc] init];
    self.courseLayout.minimumLineSpacing = CellMargin;
    self.courseLayout.minimumInteritemSpacing = 0;
    self.courseLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.courseLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.courseCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.courseLayout];
    [self.headerView addSubview:self.courseCollectionView];
    self.courseCollectionView.showsHorizontalScrollIndicator = NO;
    self.courseCollectionView.backgroundColor = [UIColor whiteColor];
    [self.courseCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.relatedCourse.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(self.dishImageView.mas_height);
    }];
    self.courseCollectionView.delegate = self;
    self.courseCollectionView.dataSource = self;
    
    [self.courseCollectionView registerNib:[UINib nibWithNibName:@"FLLRelatedCourseCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"relatedCourseCollectionCell"];
    
    // speakView
    self.speakView = [[UIView alloc] init];
    [self.headerView addSubview:self.speakView];
    [self.speakView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.courseCollectionView.mas_bottom);
        make.height.mas_equalTo(self.topView.mas_height).multipliedBy(1.2);
    }];
    
    self.
    // 发言
    self.speakLabel = [[UILabel alloc] init];
    [self.speakView addSubview:self.speakLabel];
    self.speakLabel.font = [UIFont boldSystemFontOfSize:14 * kWidthScale];
    [self.speakLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(self.agreeCount.mas_height);
    }];
    
    // 相机
    self.cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.speakView addSubview:self.cameraButton];
    [self.cameraButton setImage:[UIImage imageNamed:@"handleClass"] forState:UIControlStateNormal];
    [self.cameraButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(self.loveButton.mas_width).multipliedBy(1.2);
        make.top.right.mas_equalTo(0);
        
    }];
    
    // 交作业
    self.homeworkLabel = [[UILabel alloc] init];
    [self.speakView addSubview:self.homeworkLabel];
    [self.homeworkLabel setPropetyWithTextColor:FLLOrangeColor Alignment:1 textFont:[UIFont systemFontOfSize:10 * kWidthScale]];
    [self.homeworkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.cameraButton);
        make.top.mas_equalTo(self.cameraButton.mas_bottom);
        make.bottom.mas_equalTo(0);
    }];
    
    // 课堂发言
    self.speakButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headerView addSubview:self.speakButton];
    [self.speakButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.speakView.mas_bottom);
        make.height.mas_equalTo(self.agreeCount.mas_height).multipliedBy(1.5);
    }];


    self.detailTableView.tableHeaderView = self.headerView;
    
}
// expendButton的点击方法
- (void)didClickedExpendButton:(UIButton *)button
{
 
    if (_isExpend == NO) {
        [self.expendButton setImage:[UIImage imageNamed:@"expend_up"] forState:UIControlStateNormal];
        self.contentLabelHeight = [GetHeightTools heightWith:self.contentLabel.text];
        [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.mas_equalTo(self.topView);
            make.top.mas_equalTo(self.titleLabel.mas_bottom);
            make.height.mas_equalTo([GetHeightTools heightWith:self.contentLabel.text]);
        }];
        
        self.headerView.fll_height = 800 * kHeightScale + self.contentLabelHeight + self.choseCollectionHeight;

        _isExpend = YES;
      
    } else {
        
        [self.expendButton setImage:[UIImage imageNamed:@"expend_down"] forState:UIControlStateNormal];
        self.contentLabelHeight = 0;
        [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.mas_equalTo(self.topView);
            make.top.mas_equalTo(self.titleLabel.mas_bottom);
            make.height.mas_equalTo(0);
        }];
        _isExpend = NO;
        self.headerView.fll_height = 800 * kHeightScale + self.contentLabelHeight + self.choseCollectionHeight;
    }
    
}
- (void)didClickedUpdateButton:(UIButton *)button
{

    if (_isUpdate == NO) {
        [self.updateButton setImage:[UIImage imageNamed:@"expend_up"] forState:UIControlStateNormal];
       self.choseCollectionHeight = (self.topArray.count / 8 + 1) * self.itemHeight + self.topArray.count / 8 * CellMargin_5;
        [self.choseCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.mas_equalTo(self.topView);
            make.top.mas_equalTo(self.choseLabel.mas_bottom);
            make.height.mas_equalTo(self.choseCollectionHeight);
        }];
        _isUpdate = YES;
        [self.choseCollectionView reloadData];
        self.headerView.fll_height = 800 * kHeightScale + self.contentLabelHeight + self.choseCollectionHeight;
        
    } else {
        
        [self.updateButton setImage:[UIImage imageNamed:@"expend_down"] forState:UIControlStateNormal];
        self.choseCollectionHeight = 2 * self.itemHeight + CellMargin_5;
        [self.choseCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.mas_equalTo(self.topView);
            make.top.mas_equalTo(self.choseLabel.mas_bottom);
            make.height.mas_equalTo(self.choseCollectionHeight);
        }];
        _isUpdate = NO;
        [self.choseCollectionView reloadData];
        self.headerView.fll_height = 800 * kHeightScale + self.contentLabelHeight + self.choseCollectionHeight;
    }

    
}
- (void)createPOSTRequest
{
    
    if (![_passString containsString:@"#"]) {
        self.bodyString = [NSString stringWithFormat:@"methodName=CourseSeriesView&series_id=%@&version=4.40&", _passString];
    } else {
    self.idString = [self.passString componentsSeparatedByString:@"#"][1];
    self.bodyString = [NSString stringWithFormat:@"methodName=CourseSeriesView&series_id=%@&version=4.40&", _idString];
    }
    
    // 最上面的
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:_bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        
        self.fllDetailModel = [FLLDetailModel modelWithDic:result];
        self.topDataSource = self.fllDetailModel.data;
        self.topArray = (NSMutableArray *)self.fllDetailModel.data.data;
        self.currentIndex = self.topArray.count - 1;
        self.block();
        self.headerBlock();
        [self.detailTableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}

// 点赞
- (void)createDianZanRequestWithIndex:(NSInteger)index
{
    
    
    NSString *bodyString = [NSString stringWithFormat:@"post_id=%ld&methodName=DianzanList&media_type=3&version=4.40&", index];
    
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        
        self.fllAgreeModel = [FLLAgreeModel modelWithDic:result];
        self.userInfoArray = (NSMutableArray *)self.fllAgreeModel.data.data;
        
        [self.agreeCollectionView reloadData];
        
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
// 相关课程
- (void)createRelatedCourseRequestWithIndex:(NSInteger)index
{
    
    
    NSString *bodyString = [NSString stringWithFormat:@"--452c76cd-234d-40c6-850f-d159e6814e1b\nContent-Disposition: form-data; name=\"course_id\"\nContent-Length: 4\n\n%ld\n--452c76cd-234d-40c6-850f-d159e6814e1b\nContent-Disposition: form-data; name=\"version\"\nContent-Length: 4\n\n4.40\n--452c76cd-234d-40c6-850f-d159e6814e1b\nContent-Disposition: form-data; name=\"methodName\"\nContent-Length: 12\n\nCourseRelate\n--452c76cd-234d-40c6-850f-d159e6814e1b\nContent-Disposition: form-data; name=\"size\"\nContent-Length: 2", index];
    
    
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"multipart/form-data; boundary=452c76cd-234d-40c6-850f-d159e6814e1b"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        
        self.fllCourseRelateModel = [FLLCourseRelateModel modelWithDic:result];
        self.courseArray = (NSMutableArray *)self.fllCourseRelateModel.data.data;
        [self.courseCollectionView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
- (void)createCommentRequest
{

    if (_isLoading == NO) {
        [self.speakArray removeAllObjects];
        _pageCount = 1;
    } else {
        ++_pageCount;
    }
    
    // 评论
    if (![_passString containsString:@"#"]) {
        self.string = [NSString stringWithFormat:@"relate_id=%@&type=2&page=%zd&methodName=CommentList&size=20&version=4.40&", _passString, _pageCount];
    } else {
        self.string = [NSString stringWithFormat:@"relate_id=%@&type=2&page=%zd&methodName=CommentList&size=20&version=4.40&", _idString, _pageCount];
    }
    
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:_string andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.fllCommentModel = [FLLCommentModel modelWithDic:result];
        if (_isLoading == NO) {
            self.speakArray = (NSMutableArray *)self.fllCommentModel.data.data;
        } else {
            [self.speakArray addObjectsFromArray:(NSMutableArray *)self.fllCommentModel.data.data];
        }
        
        self.commentBlock();
        [self.detailTableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
}
- (void)createDetailTableView
{

    self.detailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_detailTableView];
    self.detailTableView.delegate = self;
    self.detailTableView.dataSource = self;
    [self.detailTableView registerNib:[UINib nibWithNibName:@"FLLDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"detailTableCell"];
    [self.detailTableView registerNib:[UINib nibWithNibName:@"FLLSpeakTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"speakTableCell"];
    
    self.detailTableView.estimatedRowHeight = 44;
    self.detailTableView.rowHeight = UITableViewAutomaticDimension;
    
    // 刷新
    self.detailTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _isLoading = NO;
        [self createCommentRequest];
        [self.detailTableView.mj_header endRefreshing];
    }];
    // 加载
    self.detailTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _isLoading = YES;
        [self createCommentRequest];
        [self.detailTableView.mj_footer endRefreshing];
    }];
    
}

#pragma mark -- TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _speakArray.count ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    FLLSpeakTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"speakTableCell"];
   
    // 头像
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:_speakArray[indexPath.row].head_img ] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    // 名字
    cell.userNameLabel.text = _speakArray[indexPath.row].nick;
    [cell.commentLabel setPropetyWithTextColor:[UIColor darkGrayColor] Alignment:0 textFont:[UIFont systemFontOfSize:15 * kWidthScale]];
    // 评论
    [cell.commentLabel setPropetyWithTextColor:[UIColor lightGrayColor] Alignment:0 textFont:[UIFont systemFontOfSize:14 * kWidthScale]];
    if (_speakArray[indexPath.row].parents.count == 0) {
        cell.commentLabel.text = _speakArray[indexPath.row].content;
    } else {
    
        self.parentsArray = (NSMutableArray *)self.speakArray[indexPath.row].parents;
        cell.commentLabel.text = [NSString stringWithFormat:@"回复 %@: %@", _parentsArray[0].nick, _parentsArray[0].content];
        
    }
    // 时间
    cell.dateLabel.text = _speakArray[indexPath.row].create_time;
    [cell.dateLabel setPropetyWithTextColor:[UIColor lightGrayColor] Alignment:0 textFont:[UIFont systemFontOfSize:15 * kWidthScale]];
    

    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return SCREEN_HEIGHT * 0.2 + [GetHeightTools heightWith:self.speakArray[indexPath.row].content ] - 20;
}

#pragma mark -- CollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (collectionView == _choseCollectionView) {
        if (_topArray.count > 16) {
            if (_isUpdate == YES) {
                return _topArray.count;
            } else {
                return 16;
            }
        }
        return _topArray.count;
    } else if (collectionView == _agreeCollectionView){
        if (_userInfoArray.count == 0) {
            return 0;
        } else {
            if (_userInfoArray.count >= 6) {
                return 6;
            }
            return _userInfoArray.count;
        }
        
    } if (collectionView == _courseCollectionView) {
        if (_courseArray.count == 0) {
            return 0;
        }
        return _courseArray.count;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView == _choseCollectionView) {
        FLLChoseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"choseCollectionCell" forIndexPath:indexPath];
    
        cell.countLabel.text = [NSString stringWithFormat:@"%ld", _topArray[indexPath.row].episode];
    
        if (indexPath.row == self.currentIndex) {
        cell.contentView.backgroundColor = [UIColor orangeColor];
        cell.countLabel.textColor = [UIColor whiteColor];
        // 上课人数
        self.inClassCount.text = [NSString stringWithFormat:@"上课人数:%ld", self.topArray[indexPath.row].video_watchcount];
        // 标题
        self.titleLabel.text = self.topArray[indexPath.row].course_name;
        // 具体内容
        self.contentLabel.text = self.topArray[indexPath.row].course_subject;
        // 菜品图片
        [self.dishImageView sd_setImageWithURL:[NSURL URLWithString:self.topArray[indexPath.row].course_image ]placeholderImage:[UIImage imageNamed:@"菜谱详情加载"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
    
        [self createDianZanRequestWithIndex:self.topArray[indexPath.row].course_id];
        [self createRelatedCourseRequestWithIndex:self.topArray[indexPath.row].course_id];
        
    } else {
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
        cell.countLabel.textColor = [UIColor blackColor];
    }

        return cell;

    } else if (collectionView == _agreeCollectionView) {

        FLLAgreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"agreeCollectionCell" forIndexPath:indexPath];
        //  点赞赋值
        self.agreeCount.text = [NSString stringWithFormat:@"%@位厨友觉得很赞", self.fllAgreeModel.data.total];
    
    
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:_userInfoArray[indexPath.row].head_img] placeholderImage:[UIImage imageNamed:@"userHeadImage-1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        return cell;
    }

    FLLRelatedCourseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"relatedCourseCollectionCell" forIndexPath:indexPath];
    [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:_courseArray[indexPath.row].relation.dishes_image] placeholderImage:[UIImage imageNamed:@"菜谱详情加载"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    cell.dishNameLabel.text = _courseArray[indexPath.row].relation.dishes_title;
    return cell;

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _choseCollectionView) {
        return CGSizeMake(_itemWidth, _itemHeight);
    
    } else if (collectionView == _agreeCollectionView) {
        return CGSizeMake(_agreeCollectionView.bounds.size.height - 2 * CellMargin_5, _agreeCollectionView.bounds.size.height - 2 * CellMargin_5);
    }
    return CGSizeMake(SCREEN_WIDTH / 2.5, _courseCollectionView.bounds.size.height);

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.currentIndex = indexPath.row;
    if (collectionView == _choseCollectionView) {
        
        [self createDianZanRequestWithIndex:_topArray[_currentIndex].course_id];
        [self createRelatedCourseRequestWithIndex:_topArray[_currentIndex].course_id];
        
        [self.userInfoArray removeAllObjects];
        [self.courseArray removeAllObjects];
        
        [self.agreeCollectionView reloadData];
        [self.courseCollectionView reloadData];
        [self.choseCollectionView reloadData];
       
        
        
    }
    
 
}












- (void)setPassString:(NSString *)passString
{

    _passString = passString;
    [self.detailTableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
